package project.pianpian.controller;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import project.pianpian.entity.User;
import project.pianpian.entity.Video;
import project.pianpian.mapper.JoinMapper;
import project.pianpian.mapper.UserMapper;
import project.pianpian.mapper.VideoMapper;
import project.pianpian.utils.ReturnUtils;

import java.util.List;

/**
 * description:
 * author :万霁初
 * date :2022/12/26 18:26
 **/
@RestController
@Slf4j
public class Main {
    @Autowired
    UserMapper userMapper;
    @Autowired
    VideoMapper videoMapper;
    @Autowired
    JoinMapper joinMapper;

    @PostMapping("/main")
    public JSONObject main(@RequestBody JSONObject jsonObject){

        //提前声明
        JSONObject returnJSON=new JSONObject();

        //获取最新的八个视频
        List<Video> videosByTimeLimit8=videoMapper.selectPageByTimeVideos();
        returnJSON.put("videos",videosByTimeLimit8);

        //获取粉丝量最高的五个用户信息
        List<User> usersByMaxFans5= joinMapper.selectUsersByMaxFans5();
        returnJSON.put("users",usersByMaxFans5);

        //获取当前登陆的用户信息，如果有登陆，查询用户信息，放入返回JSON里
        if (jsonObject.containsKey("id")){
            int id = jsonObject.getInteger("id");
            User user = userMapper.selectById(id);
            returnJSON.put("user",user);
        }

        return ReturnUtils.success(200,"获取主页内容成功",returnJSON);
    }
}
